
package net.swa.index.comm;

import java.util.Date;

import net.swa.util.DateUtils;
import net.swa.util.EncryptTool;

import org.apache.commons.lang.StringUtils;

public class IndexUtil
{
    private static final String DEFAULT_KEY = "abc";

    /**
     * 根据mac值将code加密
     * @param code
     * @param mac
     * @return
     */
    public static String generateToken(String code , String mac)
    {
        String s = null;
        if (StringUtils.isBlank(mac))
        {
            mac = DEFAULT_KEY;
        }
        try
        {
            EncryptTool tool;
            tool = new EncryptTool(mac);
            s = tool.encrypt(code);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return s;
    }

    public static String getIdFromToken(String token , String mac)
    {
        String sid = null;
        if (StringUtils.isBlank(mac))
        {
            mac = DEFAULT_KEY;
        }
        EncryptTool tool;
        try
        {
            tool = new EncryptTool(mac);
            sid = tool.decrypt(token);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return sid;
    }

    /**
     * 判断是否超出i分钟
     * @param lockedTime
     * @param i
     * @return
     */
    public static boolean isOutMinus(String lockedTime , int i)
    {
        if (StringUtils.isBlank(lockedTime))
        {
            return true;
        }
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        Date d = DateUtils.getStringDate(lockedTime, dateFormat);
        Date d2 = DateUtils.addMINUTE(-i, DateUtils.getCurrentDate());

        if (d.compareTo(d2) > 0)
        {
            return false;
        }
        else
        {
            return true;
        }

    }
}
